<?php 
require_once( "config.php" );


function sql( $statement, $typ=NULL ) {
	mysql_connect( glSqlHost, glSqlUser, glSqlPass ) or die( "Error: mysql_connect" );
	mysql_select_db( glSqlName ) or die( "Error: mysql_select_db" );
	$res = mysql_query( $statement ) or die("Error in statement: $statement");  //or die
	return $res;
}



$mapW = $_REQUEST['mapW']; 
$mapH = $_REQUEST['mapH']; 
$mapX = $_REQUEST['mapX']; 
$mapY = $_REQUEST['mapY']; 
$mapZ = $_REQUEST['mapZ']; 

$halfW = ($mapW/2);
$halfH = ($mapH/2);

$xMin = ($mapX - $halfW ) / $mapZ;
$xMax = ($mapX + $halfW ) / $mapZ;
$yMin = ($mapY - $halfH ) / $mapZ;
$yMax = ($mapY + $halfH ) / $mapZ;



// send header
header ('Content-type: image/png');

// create image object
$handle = @imagecreatetruecolor($mapW, $mapH) or die('Cannot Initialize new GD image stream');

// set text color
$text_color = imagecolorallocate($handle, 233, 14, 91);

// write infos
imagestring($handle, 2, 5, 5,  "map of ".$mapW."x".$mapH." at ".$mapX.",".$mapY, $text_color);


// draw objects
$sql = "SELECT * FROM objects WHERE X > $xMin AND X < $xMax AND Y > $yMin AND Y < $yMax ";
$result = sql($sql);
while($row = mysql_fetch_array( $result )) {
    
    // relative Positions
    $rPosX = $halfW+(($row['x']-$mapX)*$mapZ);
    $rPosY = $halfH+(($row['y']-$mapY)*$mapZ);
    
    // draw objects
    imagefilledellipse( $handle, $rPosX, $rPosY, (6*$mapZ), (6*$mapZ), $text_color );
    if($mapZ>=1) imagestring($handle, 1, $rPosX-5, $rPosY+(6*$mapZ), $row['name'], $text_color);

}

// draw center
imagefilledellipse( $handle, $halfW, $halfH, 3, 3, $text_color );



// flush infos
imagepng($handle);

// clean up object
imagedestroy($handle);



?>